package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupCheckItemDao;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.SetmealCheckGroupDao;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.dto.QueryPageBeanDTO;
import com.itheima.health.pojo.entity.CheckGroup;
import com.itheima.health.pojo.entity.CheckItem;
import com.itheima.health.pojo.result.PageResult;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private CheckGroupCheckItemDao checkGroupCheckItemDao;

    @Autowired
    private SetmealCheckGroupDao setmealCheckGroupDao;

    /**
     * 添加
     * @param checkGroup 检查组
     * @param checkItemIds
     */
    @Transactional
    @Override
    public void add(CheckGroup checkGroup,List<Integer>checkItemIds) {
        log.info("[检查组-添加]data：{},{}",checkGroup, checkItemIds);
        checkGroupDao.insert(checkGroup);
        //使用了主键返回,获取主键id
        Integer checkGroupId = checkGroup.getId();
        for (Integer checkItemId : checkItemIds) {
            //给检查组检查项关联表添加id数据
            checkGroupCheckItemDao.insertCheckItemCheckGroup(checkItemId,checkGroupId);
        }

    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBeanDTO queryPageBean) {
        log.info("[检查组-分页查询]data：{}", queryPageBean);
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        // 分页查询数据库
        Page<CheckGroup> page = checkGroupDao.selectByConditon(queryPageBean.getQueryString());
        //封装分页数据
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id删除
     * @param id
     */
    @Transactional
    @Override
    public void deleteCheckGroupitemById(Integer id) {
        log.info("[检查组-根据id删除]id：{}", id);
        //校验是否可以删除
        Long count = setmealCheckGroupDao.countSetmealByCheckGroupId(id);
        if (count > 0) {
            throw new BusinessRuntimeException("当前检查组关联了套餐，不能删除");
        }
        //删除操作
        checkGroupDao.deleteCheckGroupitemById(id);
        //把检查项检查组关联表中的数据也删除
        checkGroupCheckItemDao.deleteCheckItemCheckGroupByCheckGroupId(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> checkGroups = checkGroupDao.findAll();
        return checkGroups;
    }


    /**
     * 根据id查询
     * @param id 数据id
     * @return
     */
    @Override
    public CheckGroup findById(Integer id) {
        log.info("[检查组-根据ID查询]id:{}", id);
        return checkGroupDao.selectById(id);
    }
    /**
     * 编辑
     * @param checkGroup
     * @param checkItemIds
     */
    @Transactional
    @Override
    public void edit(CheckGroup checkGroup,List<Integer> checkItemIds) {
        log.info("[检查组-编辑]data:{},{}", checkGroup,checkItemIds);
        //更新检查组信息
        checkGroupDao.update(checkGroup);
        //先根据检查组id删除对应检查项信息
        checkGroupCheckItemDao.deleteCheckItemCheckGroupByCheckGroupId(checkGroup.getId());
        //再插入前端传来的新的检查项id
        for (Integer checkItemId : checkItemIds) {
            checkGroupCheckItemDao.insertCheckItemCheckGroup(checkItemId,checkGroup.getId());
        }
    }



    /**
     * 根据检查组id查询检查项
     * @param id
     * @return
     */
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        List<Integer> checkItemIds = checkGroupCheckItemDao.findCheckItemIdsByCheckGroupId(id);
        return checkItemIds;
    }

}
